﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using StudentLookup.BaseEntity;
using System.Net.Http;
using HtmlAgilityPack;
using StudentLookup.Utilities;

namespace StudentLookup.UserControl
{
    public partial class ResultExamDetail : PhoneApplicationPage
    {
        public ResultExamDetail()
        {
            InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string indexSelected = "";
            int index;
            string studentId = "";
            Student student = new Student();
            if (NavigationContext.QueryString.TryGetValue("studentId", out studentId))
            {
                student.StudentId = studentId;
            }

            string htmlString = "";
            using (var client = new HttpClient())
            {
                htmlString = await client.GetStringAsync(Constant.URL_ResultExam + student.StudentId);
            }
            try
            {
                HtmlDocument htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(htmlString);
                List<ResultExamination> listResultExam = new List<ResultExamination>();
                foreach (var item in htmlDocument.DocumentNode.SelectNodes("//tr[@class='kTableRow'] | //tr[@class='kTableAltRow']"))
                {
                    ResultExamination resultExam = new ResultExamination();
                    resultExam.ClassIndependentId = item.SelectSingleNode(".//td[2]").InnerText.Trim();
                    resultExam.CourseName = item.SelectSingleNode(".//td[3]/a").InnerText.Trim();
                    resultExam.CreditNumber = item.SelectSingleNode(".//td[4]").InnerText.Trim();
                    resultExam.ResultTestAvg = item.SelectSingleNode(".//td[5]").InnerText.Trim();
                    resultExam.ResultExamFirst = item.SelectSingleNode(".//td[6]").InnerText.Trim();
                    resultExam.ResultExamSecond = item.SelectSingleNode(".//td[7]").InnerText.Trim();
                    resultExam. ResultOverallAvg = float.Parse(item.SelectSingleNode(".//td[8]").InnerText.Trim());
                    resultExam.ResultExamCredit = item.SelectSingleNode(".//td[9]").InnerText.Trim();
                    resultExam.Semester = item.SelectSingleNode(".//td[11]").InnerText.Trim();
                    resultExam.Note = item.SelectSingleNode(".//td[12]").InnerText.Trim();
                    
                    listResultExam.Add(resultExam);
                }
                if (NavigationContext.QueryString.TryGetValue("indexSelected", out indexSelected))
                {
                    index = int.Parse(indexSelected);
                    txtClassIndependent.Text = listResultExam[index].ClassIndependentId;
                    txtCourseName.Text = Library.LimitWords(listResultExam[index].CourseName, 12, " ...");
                    txtCreditNumber.Text = listResultExam[index].CreditNumber;
                    txtTestAvg.Text = listResultExam[index].ResultTestAvg;
                    txtResultExam1.Text = listResultExam[index].ResultExamFirst;
                    txtResultExam2.Text = listResultExam[index].ResultExamSecond;
                    txtResultOverallAvg.Text = listResultExam[index].ResultOverallAvg.ToString();
                    txtResultCredit.Text = listResultExam[index].ResultExamCredit;
                    txtSemester.Text = listResultExam[index].Semester;
                    txtNote.Text = listResultExam[index].Note;                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}